<?php
    //初始化
    $curl = curl_init();
    //设置url
    curl_setopt($curl, CURLOPT_URL, 'http://iot-i.cn:6690/auth');
    //设置头文件的信息作为数据流输出
    //curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    $post_data = array(
        "username" => "test",
        "passwd" => "test"
        );
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
    //执行命令
    print_r("获取用户token\n");
    $data = curl_exec($curl);
    $token = json_decode($data)->token;
    //关闭URL请求
    //curl_close($curl);
    //显示获得的数据
    print_r($token);
    print_r("\n");

    // 获取设备状态
    $device_id = "m_test_5c:cf:7f:87:db:0f";
    print_r("获取设备状态\n");    
    curl_setopt($curl, CURLOPT_URL, 'http://iot-i.cn:5690/api/device/action/getstatus');
    $post_data = array(
        "device_id" => $device_id,
        "token" => $token
    );
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
    //执行命令
    $data = curl_exec($curl);
    print_r($data); 
    print_r("\n");

    // 发送消息到设备
    print_r("发送消息到设备\n");    
    curl_setopt($curl, CURLOPT_URL, 'http://iot-i.cn:5690/api/device/action/send');
    $post_data = array(
        "device_id" => $device_id,
        "token" => $token,
        "message" => "hello world"
    );
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
    //执行命令
    $data = curl_exec($curl);
    print_r($data); 
    print_r("\n");
    //关闭URL请求
    curl_close($curl);
?>
